package ms.jasim.model.editor.views;

import ms.jasim.framework.SolutionSimulator;
import ms.jasim.framework.EventSimulator.SimulatedTreeNode;
import ms.jasim.pddl.PddlSolution;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

import com.swtdesigner.SWTResourceManager;

public class SolutionView extends ViewPart implements ISelectionListener {

	private static final String NO_SOLUTION_TO_VIEW = "No solution to view";
	private StyledText styledText;
	
	public static final String ID = "ms.jasim.model.editor.views.SolutionView"; //$NON-NLS-1$
	private Label label;
	private StyledText pddlText;
	private Action autoViewSolutionAction;

	/**
	 * Create contents of the view part
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new BorderLayout(0, 0));

		Composite composite = new Composite(container, SWT.BORDER);
		composite.setLayoutData(BorderLayout.NORTH);
		composite.setLayout(new BorderLayout(0, 0));
		
		label = new Label(composite, SWT.None);
		label.setText(NO_SOLUTION_TO_VIEW);
		label.setLayoutData(BorderLayout.CENTER);
		
		final CTabFolder tabFolder = new CTabFolder(container, SWT.NONE);
		tabFolder.setTabPosition(SWT.BOTTOM);
		tabFolder.setLayoutData(BorderLayout.CENTER);

		final CTabItem textTabItem = new CTabItem(tabFolder, SWT.NONE);
		textTabItem.setText("Solution");

		styledText = new StyledText(tabFolder, SWT.V_SCROLL | SWT.READ_ONLY
				| SWT.H_SCROLL);
		textTabItem.setControl(styledText);

		final CTabItem pddlViewTabItem = new CTabItem(tabFolder, SWT.NONE);
		pddlViewTabItem.setText("Pddl View");

		pddlText = new StyledText(tabFolder, SWT.H_SCROLL | SWT.V_SCROLL);
		pddlText.setFont(SWTResourceManager.getFont("Courier New", 9, SWT.NONE));
		pddlViewTabItem.setControl(pddlText);
		
		tabFolder.setSelection(textTabItem);
		//sash.setWeights(new int[] {46, 280 });
		//
		ISelectionService service = (ISelectionService) getSite().getService(ISelectionService.class);
		service.addSelectionListener(this);
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	@Override
	public void dispose() {
		ISelectionService service = (ISelectionService) getSite().getService(ISelectionService.class);
		service.removeSelectionListener(this);
		super.dispose();
	}
	
	/**
	 * Create the actions
	 */
	private void createActions() {
		this.autoViewSolutionAction = new Action("Auto View Solution", IAction.AS_RADIO_BUTTON) {
			
		};
	}

	/**
	 * Initialize the tool bar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
		toolbarManager.add(autoViewSolutionAction);
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		//IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	public void setInput(PddlSolution solution) {
		if (solution != null) {
			styledText.setText(solution.toString());
			label.setText(solution.getName());
			pddlText.setText(solution.getProblem().toString());
		}
		else {
			styledText.setText("");
			pddlText.setText("");
			label.setText(NO_SOLUTION_TO_VIEW);
		}
	}

	
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		
		if (selection instanceof StructuredSelection) {
			StructuredSelection sel = (StructuredSelection) selection;
			Object selectItem = sel.getFirstElement();
			if (selectItem instanceof SimulatedTreeNode)
				setInput(((SimulatedTreeNode) selectItem).getSolution());
			else if (selectItem instanceof PddlSolution)
				setInput((PddlSolution) selectItem);
			else if (selectItem instanceof SolutionSimulator)
				setInput(((SolutionSimulator) selectItem).getSolution());
		}
	}

}
